<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
    // yield 暂时放弃执行  返回多次

    // yield* 相当于嵌套一个    

    function* fen(){
        yield 99;
        yield 999;
    }


    function* gen(){
        yield 1;
        yield 2;
        yield* fen();
        yield 3;
    }
    let g = gen();

    /*console.log(g);
    console.log(g.next()); // -> {value:1,done:false}
    console.log(g.next().value);
    console.log(g.next().value);
    console.log(g.next()); // -> {value:undefined,done:true}
    console.log(g.next().value);*/

    /* console.log(g);
    console.log(g.next());
    console.log(g.next());
    console.log(g.next()); */

    // Generator函数 执行next()方法 -> {value: "value",done: false}

    console.log(g.next());
    console.log(g.next());
    console.log(g.next());
    console.log(g.next());
    console.log(g.next());
    console.log(g.next());


    {
        let draw = function (count) {
            if (count===0) {
                alert('没有机会了');
                return;
            }
            console.log(`还剩余${count}次`);
            
        }

        let pull = function* (count) {
            while (count>0) {
                count --;
                yield draw(count);
            }
        }

        let start = pull(5);

        let btn = document.createElement('button');

        btn.id = 'start';

        btn.textContent = '开始';

        document.body.appendChild(btn);

        document.getElementById('start').addEventListener('click',()=>{
            start.next();
        },false);
    }


    // 
    </script>
</body>
</html>